﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using DevComponents.DotNetBar;
using System.Threading;

namespace SyncApplication.Business
{
    public abstract class AnimationBase : Object, IDisposable
    {

        public Object[] Icons { get; set; }

        private int _ImageIndex = 0;
        private int _MaxAnimationTimes = -1;
        private int _CurrentAnimationTimes = 0;
        private bool _First = true;
        public double Interval
        {
            get { return objTimer.Interval; }
            set { objTimer.Interval = value; }
        }
        bool isStop = false;


        System.Timers.Timer objTimer;

        public AnimationBase()
        {
            objTimer = new System.Timers.Timer();
            objTimer.Enabled = false;
            objTimer.Elapsed += new System.Timers.ElapsedEventHandler(objTimer_Elapsed);
            
        }


        public void Animation(int count)
        {
            isStop = false;
            if (Interval <= 0)
                throw new Exception("Invaild parameter");
            _MaxAnimationTimes = count;
            _CurrentAnimationTimes = 0;
            objTimer.Start();
        }
        public void Stop()
        {
            isStop = true;
            objTimer.Enabled = false;
        }
        protected abstract void InitIcon();


        void objTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (isStop)
                return;

            if (_ImageIndex >= Icons.Length)
                _ImageIndex = 0;
            SetIcon(Icons[_ImageIndex]);
            _CurrentAnimationTimes = _MaxAnimationTimes > 0 ? _CurrentAnimationTimes++ : 0;
            if (_MaxAnimationTimes > 0 && _MaxAnimationTimes < _CurrentAnimationTimes)
                objTimer.Stop();
            _ImageIndex++;
        }

        protected abstract void SetIcon(object icon);

        protected abstract void ClearIcon();
        #region IDisposable Members

        public void Dispose()
        {
            objTimer.Dispose();
            Thread.Sleep(1000);
            ClearIcon();
        }

        #endregion
    }
    public class LabelXAnimation : AnimationBase
    {
        public LabelItem Label { get; set; }
        public string IconPath { get; set; }
        public LabelXAnimation(string path)
            : base()
        {

            //InitIcon();
        }
        public void LoadIcon()
        {
            InitIcon();
        }
        protected override void ClearIcon()
        {
            for (int i = 0; i < Icons.Length; i++)
            {
                ((Image)Icons[i]).Dispose();
            }
            Icons = null;
        }
        protected override void InitIcon()
        {
            string[] files = Directory.GetFiles(IconPath, "*.png");
            List<Image> lstIcon = new List<Image>();
            foreach (string node in files)
            {
                lstIcon.Add(Image.FromFile(node));
            }
            Icons = lstIcon.ToArray();
        }

        protected override void SetIcon(object icon)
        {
            this.Label.Image = (Image)icon;
        }
    }
}
